﻿using System;
using System.Linq;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using Entity.Validation.Core;

namespace WPFEntityValidation.Converters
{
    public class NullObjectToVisibiltyConverter : MarkupExtension, IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    public class NullObjectToBoolConverter : MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return false;

            if (value is IList)
            {
                return ((IList)value).Count != 0;
            }

            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
    public class ToLowerConverter
          : MarkupExtension, IValueConverter
    {
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                var strValue = value.ToString();


                return strValue.ToLowerInvariant();
            }
            return null;
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    public sealed class BoolToVisibilityConverter : MarkupExtension, IValueConverter
    {
        private bool _useHiddenVisibility = false;
        private bool _negateInputValue = false;

        public bool UseHiddenVisibility
        {
            get { return _useHiddenVisibility; }
            set { _useHiddenVisibility = value; }
        }

        public bool NegateInputValue
        {
            get { return _negateInputValue; }
            set { _negateInputValue = value; }
        }

        #region IValueConverter Members

        public  object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var notVisible = (UseHiddenVisibility) ? Visibility.Hidden : Visibility.Collapsed;

            if (value == null)
                return notVisible;
            if (value is bool)
            {
                var boolValue = (this.NegateInputValue) ? !(bool)value : (bool)value;
                return boolValue ? Visibility.Visible : notVisible;
            }

            return notVisible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool && (bool)value)
                return Visibility.Visible;

            return (this.UseHiddenVisibility) ? Visibility.Hidden : Visibility.Collapsed;
        }

        #endregion

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    public class ValidateFieldConverter : MarkupExtension, IValueConverter
    {
        public String PropertyName { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is IValidateEntity)
            {
                return
                    ((IValidateEntity) value).ValidateEntities()
                                             .Where(x => x.ToLower().Contains(this.PropertyName.ToLower()));
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
}
